﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace WebApplication2
{
    public class SQLHelper
    {
        private string constr = "server=LAPTOP-KBF70ACQ;uid=sa;password=2324ln;database=Student_db";
        private SqlConnection con;
        public SQLHelper()
        {
            con = new SqlConnection(constr);
        }

        public DataSet Get(string sql, SqlParameter[] pars)
        {
            DataSet ds = null;
            try
            {
                Console.WriteLine((int)con.State);
                if ((int)con.State == 0)
                {

                    con.Open();
                }
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddRange(pars);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                ds = new DataSet();
                adapter.Fill(ds);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                con.Close();
            }
            return ds;
        }

        public int Add(string sql, SqlParameter[] pars)
        {
            int result = -1;
            try
            {
                Console.WriteLine((int)con.State);
                if ((int)con.State == 0)
                {

                    con.Open();
                }
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddRange(pars);
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                con.Close();
            }
            return result;
        }
    }
}